﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenMain : MonoBehaviour
{
    
    public GameObject Pixel;
    // x,y轴有多少个点
    public readonly int _screenW = 10;
    public readonly int _screenH = 20;
    // 方块的大小
    readonly int _pixel_W_H = 14;
    // 方块之间的间隔
    readonly int _interval = 1;
    // GameObject[,] _screen = new GameObject[_screenW, _screenH];
    GameObject[,] screen;
    List<Layer> _layerList = new List<Layer>();
    
    // 保存上一次的屏幕视图, 用于刷新屏幕
    List<MyPoint> oldListPoint;
    public ScreenMain() {
        screen = new GameObject[_screenW, _screenH];
    }
    // 初始化方法
    public void init() {
       initLayer();
       initScreen();
    }

    // 初始化图层
    void initLayer() {
        // 新建默认图层
        Layer layerDefault = new Layer();
        layerDefault.name = "layerDefault";
        _layerList.Add(layerDefault);
    }
    // 初始化屏幕
    void initScreen() {
        for (int xTemp = 0; xTemp < _screenW; xTemp++)
        {
            for (int yTemp = 0; yTemp < _screenH; yTemp++)
            {
                 GameObject pixel = Instantiate(Pixel, new Vector3(xTemp * (_pixel_W_H + _interval), yTemp * (_pixel_W_H + _interval), 0) + transform.position, Quaternion.identity) as GameObject;
                 pixel.SetActive(false);
                 pixel.transform.parent = transform;
                 screen[xTemp, yTemp] = pixel;
            }
        }
    }
    // 添加图层
    public void addLayer(string layerName) {
        Layer layer = new Layer();
        layer.name = layerName;
        layer.point = new MyPoint(Mathf.RoundToInt(_screenW / 2), _screenH);
        _layerList.Add(layer);
    }
    // 获取图层
    public Layer getLayer(string layerName) {
        foreach (Layer layer in _layerList)
        {
            if (layer.name == layerName)
            {
                return layer;
            }
        }
        return null;
    }
    // 删除图层
    public void removeLayer(string layerName) {
        for (int i = 0; i < _layerList.Count; i++)
        {
            Layer layer = _layerList[i];
            if (layer.name == layerName) {
                _layerList.RemoveAt(i);
            }   
        }
    }
    // 刷新屏幕
    public void refScreenMain() {
        // 擦去旧的显示数据
        if (oldListPoint != null) {
            foreach (var item in oldListPoint)
            {
                // 判断是否超出屏幕
                if (item._x > 9 || item._y > 19 || item._x < 0 || item._y < 0) {
                    continue;
                }
                screen[item._x, item._y].SetActive(false);
            }
        }

        // 判断停下来
        // 首先判断是否到底部了
        Layer layerT = getLayer("layerT");
        Layer layerDefault = getLayer("layerDefault");
        List<MyPoint> temp = new List<MyPoint>();
        if (layerT != null) {
            foreach (var item in layerT.viewData) {   
                temp.Add(new MyPoint(item._x + layerT.point._x, item._y + layerT.point._y));
            }

            foreach (var item in temp)
            {
                // 到达底部了
                if (item._y <= 0) {
                    layerDefault.viewData.AddRange(temp);
                    removeLayer(layerT.name);
                    Debug.Log(layerT);
                    // layerT = null;
                    // layerT = null;
                }   
                // 判断下面有个点
                MyPoint pointTemp = new MyPoint(item._x, item._y - 1);
                if (layerDefault.viewData.Contains(pointTemp)) {
                    layerDefault.viewData.AddRange(temp);
                    removeLayer(layerT.name);
                    // layerT = null; 
                }
            }
        }
        
        // 把所有的点集中到一个数组中
        List<MyPoint> viewData = new List<MyPoint>();
        foreach (Layer layer in _layerList) {
            foreach (MyPoint point in layer.viewData) {
                MyPoint pointTemp = new MyPoint(point._x + layer.point._x, point._y + layer.point._y);
                viewData.Add(pointTemp);
            }
        }

        foreach (MyPoint item in viewData) {
            // 判断是否超出屏幕
            if (item._x > 9 || item._y > 19 || item._x < 0 || item._y < 0) {
                continue;
            }
            screen[item._x, item._y].SetActive(true);
        }
        oldListPoint = viewData;
    }
}
